﻿using Newtonsoft.Json;
using Survey.DataAccessLayer;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Business;

namespace WebUI.Controllers
{
    public class ApiController : Controller
    {
        #region 用户管理
        private UserBusiness userBusiness = new UserBusiness();
        // Api/LoginUser
        [HttpPost]
        public JsonResult LoginUser(User user)
        {
            return Json(userBusiness.LoginUser(Session, user));
        }
        // Api/EditUser
        [HttpPost]
        public JsonResult EditUser(User tempUser)
        {
            return Json(userBusiness.EditUser(Session, tempUser));
        }
        //Api/regUser
        [HttpPost]
        public JsonResult RegUser(User user)
        {
            return Json(userBusiness.RegUser(user));
        }
        //api/LoginOutUser
        public JsonResult LoginOutUser()
        {
            return Json(userBusiness.LoginOutUser(Session), JsonRequestBehavior.AllowGet);
        }
        // api/DetailedUser
        public JsonResult DetailedUser()
        {

            return Json(userBusiness.DetailedUser(Session), JsonRequestBehavior.AllowGet);
        }
        #endregion


        #region Helper
        //解决Self loop问题
        private String Obj2Json(Object obj)
        {

            JsonSerializerSettings setting = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Formatting = Formatting.None
            };
            var ret = JsonConvert.SerializeObject(obj, setting);
            return ret;
        }
        #endregion


        #region 问卷管理
        private QuestionnaireBusiness questionnaireBusiness = new QuestionnaireBusiness();
        //api/EditQuestionnaireById
        [HttpGet]
        public JsonResult EditQuestionnaireById(int id)
        {

            return Json(questionnaireBusiness.EditQuestionnaireById(Session, id), JsonRequestBehavior.AllowGet);

        }
        //api/CreateQuestionnaire
        [HttpPost]
        public JsonResult CreateQuestionnaire(Questionnaire questionnaire)
        {

            return Json(questionnaireBusiness.CreateQuestionnaire(Session, questionnaire));
        }
        //api/DeleteQuestionnaireById
        [HttpPost]
        public JsonResult DeleteQuestionnaireById(int id)
        {
            return Json(questionnaireBusiness.DeleteQuestionnaireById(Session, id));
        }
        //api/PostEditedQuestionnaire
        [HttpPost]
        public JsonResult PostEditedQuestionnaire(Questionnaire q)
        {

            return Json(questionnaireBusiness.PostEditedQuestionnaire(Session, q));
        }
        #endregion


        #region 答卷页面
        QuestionFeedbackBusiness questionFeedbackBusiness = new QuestionFeedbackBusiness();
        //api/PostQuestionFeedback
        public JsonResult PostQuestionFeedback(List<int> IdCollection)
        {

            return Json(questionFeedbackBusiness.PostQuestionFeedback(IdCollection));
        }
        //api/GetQuestionnaireById
        public JsonResult GetQuestionnaireById(int id)
        {
            return Json(questionFeedbackBusiness.GetQuestionnaireById(id), JsonRequestBehavior.AllowGet);
        }
        // api/GetAllQuestionnaire
        public JsonResult GetAllQuestionnaire()
        {
            return Json(questionFeedbackBusiness.GetAllQuestionnaire(Session),JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}